import { PiniaPluginContext } from 'pinia'
import { storeIds } from '@/store/ids'
import { setItem, removeItem, getItem } from '@/utils/storage'
import { USER_KEY } from '@/store/storage-key'

export const userPlugins = (context: PiniaPluginContext) => {
  console.log('--------------------------------')
  console.log(context)
  if (storeIds.user === context.store.$id) {
    console.log('用户')
    const user = getItem(USER_KEY)
    context.store.$onAction((actionEvent) => {
      console.log('============================')
      console.log(actionEvent)
      if(actionEvent.name === 'login') {
        setItem(USER_KEY, JSON.stringify(actionEvent.args[0]))
      } else if(actionEvent.name === 'logout') {
        removeItem(USER_KEY)
      }
    })
    return {
      user
    }
  }
}

